package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

type MyMux struct {
}

func (p *MyMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path == "/" {
		sayhello(w, r)
		return
	}
	http.NotFound(w, r)
	return
}

func sayhello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "<h1>hello world</h1>")
}

func main() {
	//1、请求
	resp, _ := http.Get("http://www.baidu.com")

	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	//将[]byte转换为string   反过来   []byte(变量名)
	//fmt.Println(string(body))
	//或者使用格式化打印，直接打印是byte
	fmt.Printf("%s", body)
	fmt.Println("done")

	//2、web服务器
	mux := &MyMux{} //实例化一个，然后将指针传递给mux
	http.ListenAndServe(":9090", mux)

}
